﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Images;
using Silvermoon.Animations;
using Silvermoon.UI;
using System.Drawing;
using OutlookHub.Utils;
using Silvermoon.OpenGL;
using Microsoft.WindowsMobile.PocketOutlook;
using Silvermoon.Windows;
using Silvermoon.Shapes;
using OutlookHub.Images;

namespace OutlookHub.Controls
{
    public class TaskItem : ContainerItem
    {
        public static Command TaskCompleteCommand = new Command();
        private CheckBox checkBox;
        private Label subjectLabel, durationLabel;
        private PictureBox imageBox;

        public TaskItem()
            : base()
        {
            checkBox = new CheckBox { Size = new Size(72, 72), Background = SysImages.Circle, Margin = new Margin(4, 0, null, null) };
            subjectLabel = new Label { Font = FontPool.MediumRegularFont, Margin = new Margin(78, 0, 24, 0), LineAlignment = Alignment.Near, Alignment = Alignment.Near };
            durationLabel = new Label { FontScale = 0.60f, Margin = new Margin(78, FontPool.MediumRegularFont.CharHeight, 24, 0), LineAlignment = Alignment.Near, Alignment = Alignment.Near, Opacity = 190, Skew = 2 };
            imageBox = new PictureBox { Margin = new Margin(null, 0, 0, null), Size = new Size(24, 24), Visible = false, UseTextColor = true };
            Panel p = new Panel { Margin = Margin.Zero, Height = 90 };
            Control = p;
            p.Controls.Add(checkBox, subjectLabel, durationLabel, imageBox);
            checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
        }


        public string Subject
        {
            get { return subjectLabel.Text; }
            set { subjectLabel.Text = value; }
        }

        public string Details
        {
            get { return durationLabel.Text; }
            set { durationLabel.Text = value; }
        }

        public bool IsComplete
        {
            get { return checkBox.IsChecked; }
            set { checkBox.IsChecked = value; }
        }

        protected override void OnDataChanged()
        {
            Task task = DataValue as Task;
            if (task != null)
            {
                Subject = task.Subject;
                Details = task.ToDuration();
                IsComplete = task.Complete;
                imageBox.Visible = task.Importance != Importance.Normal;
                imageBox.TextColor = GetPrioColor(task.Importance);
                imageBox.Image = GetPrioImage(task.Importance);
            }
            base.OnDataChanged();
        }

        private Color GetPrioColor(Importance importance)
        {
            switch (importance)
            {
                case Importance.High: return PhoneColors.Red;
                case Importance.Low: return PhoneColors.Blue;
                default: return PhoneColors.Text;
            }
        }

        private Texture GetPrioImage(Importance importance)
        {
            switch (importance)
            {
                case Importance.Normal: return null;
                case Importance.Low: return ImagePool.LowPriority;
                case Importance.High: return ImagePool.HighPriortiy;
                default: throw new NotSupportedException();
            }
        }

        void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            IsComplete = checkBox.IsChecked;
            Task task = DataValue as Task;
            if (task != null)
            {
                if (task.Complete != IsComplete)
                {
                    task.Complete = IsComplete;
                    task.Update();
                    RaiseCommand(TaskCompleteCommand);
                }
            }
        }


    }
}
